import time
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from Utils import TestDriver


class TestRegister:
    url = ""
    driver = ""
    def __init__(self):
        self.driver = TestDriver.driver
        self.url = "http://8.155.1.153:8081/user/register"
        self.driver.get(self.url)

    def RegisterSuccess(self):
        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys("xiaohua")
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("xiaohua")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#rePassword").send_keys("123456")
        #点击注册
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()
        #注册成功返回登陆页面时,验证是不是登陆界面
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")
        #添加截图
        TestDriver.getScreenShot()

    def RegisterFail(self):
        test_dic = [
            {"desc": "密码不足6位", "name": "test_user", "username": "test_username", "password": "123",
             "rePassword": "123"},
            {"desc": "不写用户名注册", "name": "", "username": "test_username", "password": "123456",
             "rePassword": "123456"},
            {"desc": "不写登录账号注册", "name": "test_user", "username": "", "password": "123456",
             "rePassword": "123456"},
            {"desc": "不写密码注册", "name": "test_user", "username": "test_username", "password": "",
             "rePassword": ""},
            {"desc": "不写密码确认注册", "name": "test_user", "username": "test_username", "password": "123456",
             "rePassword": ""}
        ]

        for dic in test_dic:
            # 清空输入框
            element = self.driver.find_element(By.CSS_SELECTOR, "#name")
            element.send_keys(Keys.CONTROL + 'a')  # 输入框看着有内容，实际是隐藏字符或浏览器自动填充，clear 方法对这类情况处理不好。
            element.send_keys(Keys.DELETE)  # 先全选，再删除的方式清空输入栏
            element = self.driver.find_element(By.CSS_SELECTOR, "#username")
            element.send_keys(Keys.CONTROL + 'a')
            element.send_keys(Keys.DELETE)
            element = self.driver.find_element(By.CSS_SELECTOR, "#password")
            element.send_keys(Keys.CONTROL + 'a')
            element.send_keys(Keys.DELETE)
            element = self.driver.find_element(By.CSS_SELECTOR, "#rePassword")
            element.send_keys(Keys.CONTROL + 'a')
            element.send_keys(Keys.DELETE)
            time.sleep(3)

            #输入用户名
            if dic["name"]:
                time.sleep(1)
                self.driver.find_element(By.CSS_SELECTOR,"#name").clear()
                time.sleep(1)
                self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys(dic["name"])

            #输入登陆账号
            if dic["username"]:
                time.sleep(1)
                self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
                time.sleep(1)
                self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys(dic["username"])

            #输入密码
            if dic["password"]:
                time.sleep(1)
                self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
                time.sleep(1)
                self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys(dic["password"])

            #输入密码确认
            if dic["rePassword"]:
                time.sleep(1)
                self.driver.find_element(By.CSS_SELECTOR,"#rePassword").clear()
                time.sleep(1)
                self.driver.find_element(By.CSS_SELECTOR,"#rePassword").send_keys(dic["rePassword"])

            #点击注册
            self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()
            #验证注册账号的按钮是否存在
            self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")
            time.sleep(3)
            #添加截图
            TestDriver.getScreenShot()